<?php

class Forms
{

    public $conn_;

    public function __construct($dblink)
    {

        $this->conn_ = $dblink;
    }

    public function create_insert_form($table, $fk)
    {
        $CLASS_QUERIES = new Queries($this->conn_);

        $query = $this->conn_->query("SELECT * FROM $table");
        $fields = $query->fetch_fields();
        if (!empty($fk)) {
            $fk_items = $CLASS_QUERIES->get_items_fk($fk);
        }
        $form = "<form name='" . $table . "' action='" . $table . ".php' method='POST' enctype='multipart/form-data' >";

        foreach ($fields as $field) {
            $placeholder = str_replace("_", " ", ucfirst(strtolower($field->name)));
            $name = $field->name;

            if ($field->type == 3 AND array_key_exists($name, $fk)) {
                $form .= "<select id='" . $name . "' name='" . $name . "' data-placeholder='Select " . $name . "'>";
                $form .= $fk_items;
                $form .= "</select>";
                $form .= "<script type='text/javascript'>
                            $(document).on('ready',function(){
                               $('#$name').select2();
                            });

                            </script>";
            } elseif ($field->type == 253 and $name != 'file') {
                $form .= "<input type='text' name='" . $name . "' placeholder='" . $placeholder . "' >";
            } elseif ($field->type == 5) {
                $form .= "<input type='text' name='" . $name . "' placeholder='" . $placeholder . "' >";
            } elseif ($field->type == 252) {
                $form .= "<script type='text/javascript'>
                            $(document).on('ready',function(){
                               $('#$name').jqte();                                 
                            });

                            </script>";

                $form .= "<textarea id='" . $name . "' name='" . $name . "' placeholder='" . $placeholder . " '></textarea>";
            } elseif (strstr('file', $name)) {
                $form .= "$placeholder<input type='file' name='" . $name . "' >";
            }
        }

        $form .= "<input type='hidden' name='insert' >
                    <input type='submit' value='SEND' >
                    </form>";

        return $form;
    }
}

?>